package org.hcyspartnoc.luoxin.controller.service;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.asset.AssetVCounselorException;
import org.hcyspartnoc.luoxin.common.exception.framework.AuthException;
import org.hcyspartnoc.luoxin.common.exception.service.ServiceCounselorOrderException;
import org.hcyspartnoc.luoxin.common.exception.service.WeiXinException;
import org.hcyspartnoc.luoxin.pojo.dto.service.ServiceCounselorOrderAddDto;
import org.hcyspartnoc.luoxin.pojo.po.service.ServiceCounselorOrderPo;
import org.hcyspartnoc.luoxin.pojo.vo.service.ServiceCounselorOrderVo;
import org.hcyspartnoc.luoxin.service.service.IServiceCounselorOrderService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/service/cOrder")
public class ServiceCounselorOrderController {
    @Resource
    private IServiceCounselorOrderService serviceCounselorOrderService;

    @Log(title = "服务·咨询师订单·新增", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "service:cOrder:add", descr = "新增咨询师订单")
    @PostMapping("/create")
    public AjaxResult createCOrder(@Valid @RequestBody ServiceCounselorOrderAddDto serviceCounselorOrderAddDto) throws WeiXinException, ServiceCounselorOrderException, AssetVCounselorException {
        ServiceCounselorOrderVo serviceCounselorOrderVo = serviceCounselorOrderService.createCOrder(serviceCounselorOrderAddDto);
        return AjaxResult.success(serviceCounselorOrderVo);
    }

    @Log(title = "服务·咨询师订单·取消订单", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "service:cOrder:cancel", descr = "取消订单")
    @PutMapping("/cancel/{orderId:\\d+}")
    public AjaxResult cancelOrder(@PathVariable Long orderId) throws ServiceCounselorOrderException, AssetVCounselorException, AuthException {
        serviceCounselorOrderService.cancelOrder(orderId);
        return AjaxResult.successMsg();
    }

    @Log(title = "服务·咨询师订单·查询·分页·token", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "service:cOrder:query:page:token", descr = "分页查询咨询师订单")
    @GetMapping("/query/page/token")
    public AjaxResult queryPageByToken(@Min(value = 1, message = "当前页不能小于1")
                                       @RequestParam(defaultValue = "1") int currentPage,
                                       @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                       @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @Min(value = 0, message = "状态值0-5")
                                       @Max(value = 5, message = "状态值0-5")
                                       @RequestParam(defaultValue = "0") Integer status) {
        PageBean<ServiceCounselorOrderPo, ServiceCounselorOrderVo> pageBean = serviceCounselorOrderService.queryPageByToken(currentPage, pageSize, status);
        return AjaxResult.success(pageBean);
    }

    @Log(title = "服务·咨询师订单·查询·id", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "service:cOrder:query:id", descr = "通过id查询订单信息")
    @GetMapping("/query/{orderId:\\d+}")
    public AjaxResult queryById(@PathVariable Long orderId) throws ServiceCounselorOrderException, AuthException {
        ServiceCounselorOrderVo serviceCounselorOrderVo = serviceCounselorOrderService.queryById(orderId);
        return AjaxResult.success(serviceCounselorOrderVo);
    }

    @Log(title = "服务·咨询师订单·开启订单", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "service:cOrder:start", descr = "开启订单")
    @PutMapping("/start/{orderId:\\d+}")
    public AjaxResult startOrder(@PathVariable Long orderId) throws ServiceCounselorOrderException, AuthException {
        serviceCounselorOrderService.startOrder(orderId);
        return AjaxResult.successMsg();
    }



}
